"""
@name : 3.contextlib实现上下文管理器
@author : wenyao
@projectname: sanchuanglianxi
"""
#不需要手写class 实现上下文管理器
#使用contextlib 实现
import contextlib

#@contextlib.contextmanager
# def context():
#     print("entering the zone")  #__enter__
#     try:
#         yield
#     except Exception as e:    #__exit__
#         print(f"with error {e}")
#     else:
#         print("with no error")

@contextlib.contextmanager
def context():
    print("enter  context")  #__enter__
    yield
    print("exit  context")   #__exit__

with context():
    print("use context")
    # raise ValueError

#课后作业
#run_command("ls")